#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib

import pymysql

conn = pymysql.connect(host='122.228.252.77', user='root', password='wifi@2016', port=3306, db='vpn')
cursor = conn.cursor()


def set_password(sql1, sql2):
    try:
        cursor.execute(sql1)
        conn.commit()
        cursor.execute(sql2)

        results = cursor.fetchall()

        if results[0][0] == password:
            print('修改成功')
        else:
            print('修改失败')
    except Exception as e:
        conn.rollback()
        print('操作失败：%s' % e)
    finally:
        cursor.close()
        conn.close()


def pwd_to_md5(pwd):
    m = hashlib.md5()
    pwd_bytes = pwd.encode('utf-8')
    m.update(pwd_bytes)

    return m.hexdigest()


if __name__ == '__main__':
    password = "admin123"
    password = pwd_to_md5(password)
    mobile = 18674447635

    sql1 = """update rbac_userinfo set password='{}' where mobile='{}'""".format(password, mobile)
    sql2 = "select password from rbac_userinfo where mobile={}".format(mobile)

    set_password(sql1, sql2)
